' 
' Copyright (C) 2014 Infinity CCS Ltd (http://www.infinityccs.com/)
' Copyright (C) 2007 Sasa Coh <sasacoh@gmail.com>
'
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program.  If not, see <http://www.gnu.org/licenses/>. 
'/


Imports Infinity.pjsipNET.Common
Imports Infinity.pjsipNET.Common.CallState
Imports Infinity.pjsipNET.Common.Media

Namespace CallControl.CallState

#Region "Incoming"
  ''' <summary>
  ''' Incoming state indicates incoming call. Check CFx and DND features. Start ringer. 
  ''' </summary>
  Friend Class Incoming
    Inherits IState

    Public Sub New(sm As CallMachine)
      MyBase.New(sm)
      Id = EStateId.INCOMING
    End Sub

    Friend Overrides Sub OnEntry()
      '// set incoming call flags
      _smref.Incoming = True

      CallProxy.alerted()
      _smref.Type = CallLog.ECallType.EMissed
      MediaProxy.playTone(ETones.EToneRing)

      '// if CFNoAnswerFlag active start timer
      If (_smref.Config.Account(_smref.AccountId).CFNoAnswer AndAlso _smref.Config.Account(_smref.AccountId).CFNoAnswerTimeout > 0 AndAlso _smref.Config.Account(_smref.AccountId).CFNoAnswerNumber.Length > 0) Then
        _smref.StartTimer(Timer.ETimerType.ENOANSWER, _smref.Config.Account(_smref.AccountId).CFNoAnswerTimeout)
      Else
        '// Start no response timer
        _smref.StartTimer(Timer.ETimerType.ENORESPONSE, 60) '1minute to answer before automatic hangup
      End If

      '// auto answer call (if single call)
      If ((_smref.Config.AAFlag = True) AndAlso (_smref.NumberOfCalls = 1)) Then
        Me.acceptCall()
      End If
    End Sub

    Friend Overrides Sub OnExit()
      MediaProxy.stopTone()
      _smref.StopAllTimers()
    End Sub

    Public Overrides Function acceptCall() As Boolean
      _smref.Type = CallLog.ECallType.EReceived
      _smref.Time = System.DateTime.Now

      CallProxy.acceptCall()
      _smref.ChangeState(EStateId.ACTIVE)
      Return True
    End Function

    Public Overrides Sub onReleased()
      _smref.ChangeState(EStateId.RELEASED)
    End Sub

    Public Overrides Function xferCall(number As String) As Boolean
      '// In fact this is not Tranfser. It's Deflect => redirect...
      Return CallProxy.serviceRequest(CInt(EServiceCodes.SC_CD), number)
    End Function

    Public Overrides Function endCall() As Boolean
      CallProxy.serviceRequest(CInt(EServiceCodes.SC_DND), "")
      _smref.ChangeState(EStateId.TERMINATED)
      CallProxy.endCall()
      Return MyBase.endCall()
    End Function

    ''' <summary>
    ''' No reply timer expired. Send service code to call proxy.
    ''' </summary>
    ''' <param name="sessionId"></param>
    ''' <returns></returns>
    Public Overrides Function noAnswerTimerExpired(sessionId As Integer) As Boolean
      CallProxy.serviceRequest(CInt(EServiceCodes.SC_CFNR), _smref.Config.Account(_smref.AccountId).CFNoAnswerNumber)
      Return True
    End Function

    ''' <summary>
    ''' Response timer expired. Releasing the call...
    ''' </summary>
    ''' <param name="sessionId"></param>
    ''' <returns></returns>
    Public Overrides Function noResponseTimerExpired(sessionId As Integer) As Boolean
      _smref.ChangeState(EStateId.TERMINATED)
      CallProxy.endCall()
      Return True
    End Function
  End Class
#End Region

End Namespace
